import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import json
import csv
from pymongo import MongoClient
import re
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def excel_pattern()  :  # 淘宝商家的省份分析
    data=pd. read_excel('男裤信息.xls')
    provinces=[ '重庆','湖南', '山西', '安徽', '江西', \
               '河南', '湖北','四川','天津','河北','山东', '北京', '上海', '江苏', '福建', '浙江', '广东']
    c=np. zeros(17)
    for i in range(len(data)):
        item=data[ '商品产地'][i]
        j=0
        for place in provinces:
            j+=1
            c1 = re.compile(place).findall(str(item))
            if c1!=[]:
                # print(j)
                c[j-1]+=1
            else:
                continue
    index =np. arange(1,18)
    plt.barh(index, c)
    plt.title('淘宝商家的省份分布')
    plt.yticks(index+0.1, provinces)
    plt.show()

#excel_pattern()

def picher():#饼型图
    data = pd.read_excel('男裤信息.xls')
    shop_names=['旗舰店','专营店','专卖店','其他']
    c = np.zeros(4)
    cs=len(data)
    for i in range(cs):
        item = data['商品店名'][i]
        j = 0
        for name in shop_names:
            j += 1
            re_name='.+?'+name
            c1 = re.compile(re_name).findall(str(item))
            if c1 != []:
                c[j - 1] += 1
                data=data.drop(i)
            else:
                continue
    c[-1]=len(data)

    for n in range(len(shop_names)):

        ds = c[n]/cs*100
        print(ds)
        shop_names[n]=shop_names[n]+'%d%%'%ds
    print(shop_names)
    colors=['blue','green','red','yellow']
    plt.title('淘宝商店名比例')
    plt.pie(c,labels=shop_names,colors=colors)
    plt.axis('equal')
    plt.show()

#picher()

def tiao():#水平条状图
    data = pd.read_excel('男裤信息.xls')
    c = np.zeros(6)
    for i in range(len(data)):
        item=data[ '商品价格'][i]
        if item <= 60:
            c[5] += 1
        elif 60 < item <= 110:
            c[4]+= 1
        elif 110 < item <= 160:
            c[3] += 1
        elif 160 < item <= 210:
            c[2] += 1
        elif 210 < item < +260:
            c[1] += 1
        else:
            c[0] += 1
    index = [1, 2, 3, 4, 5, 6]
    plt.barh(index, c)
    plt.title('淘宝商品价格区间')
    plt.yticks(index, ['大于260元','210元到260元',   '160元到210元','110元到160元',  '60元到110元','小于60元'])
    plt.show()
#tiao()
def tiaop():#频数图
    data = pd.read_excel('男裤信息.xls')
    item=data[ '有多少人付款']
    #print(item)
    x1 = np.arange(len(data))
    y1 = item
    plt.title('销量与商品位置的关系')
    plt.ylabel('销量')
    plt.xlabel('位置')
    plt.plot(x1, y1)
    plt.show()

tiaop()